<div id='ajaxTable_html' style='display:none'></div>
<div id="ajax_edit_win"></div>
<div id="ajax_query_win"></div>
<script>
function _ajaxRichGrid(responseText,ajaxTable,toolbar,title){
	var htmlDiv=Ext.get("ajaxTable_html").dom;
	htmlDiv.innerHTML=responseText;
	var span=Ext.get(ajaxTable+"_columns").dom;
	var columns=Ext.decode(span.innerText);
	var gridColumns=[];
	Ext.each(Ext.ux.clone(columns),function(col){
		if(col.gridDisplay){
		gridColumns.push(col);
		} 
	});
	var formColumns=Ext.ux.clone(gridColumns);
	var queryColumns=[];
	Ext.each(Ext.ux.clone(columns),function(col){
		if(col.allowQuery){
			col.allowBlank=true;
			queryColumns.push(col);
		} 
	});
	var richGrid= new Ext.quick.QuickGrid(ajaxTable, {
		id:'c_grid',
		split:true,
		stripeRows: true, 
		enableDragDrop:false,
		tbar:toolbar,
		title:title,
		clicksToEdit:1,
		'remove':true,
		height:g_height*.85,
		'smConfig':{}
	},gridColumns);
	
	globalScope.grid.add(ajaxTable,richGrid);
	
	var queryForm = new Ext.quick.QuickForm({
		labelAlign: 'right',
		title: "<c:out value='${model.title}'/>--<fmt:message key="label.query"/>",
		frame:true,
		url:'form.jsp'
	},queryColumns,2);
	globalScope.queryForm.add(ajaxTable,queryForm);
	
	Ext.get('ajax_query_win').dom.innerHTML="";
	var queryWin = new Ext.Window({
		el:'ajax_query_win',
		layout:'fit',
		width:800,
		height:600,
		closeAction:'hide',
		buttonAlign:'center',
		items: [queryForm],
		buttons: [{
			text:'<fmt:message key="button.query"/>',
			tabIndex:queryForm.lastTabIndex,
			handler:function(){
	            var postData=queryForm.getForm().getValues();
	            Ext.apply(queryWin,{'queryData':postData});
	            if(Ext.isEmpty(queryWin.beforeQuery)==false){
	                queryWin.beforeQuery(postData);
	            }
	            globalScope._model(postData,function(responseText){
				    htmlDiv.innerHTML=responseText;
				    var htmlTable=Ext.get(ajaxTable);
				    globalScope.grid.get(ajaxTable).reloadData(htmlTable);	
		            if(Ext.isEmpty(queryWin.afterQuery)==false){
		                queryWin.afterQuery(responseText);
		            } 		        
 		        },false);
				queryWin.hide();
			}
		},{
	        text:'<fmt:message key="button.reset"/>',
	        handler:function(){queryForm.getForm().reset()}
	    },{
				text:'<fmt:message key="button.cancel"/>',
				tabIndex:queryForm.lastTabIndex+1,
				handler:function(){queryWin.hide()}
		}]
	});
	Ext.apply(queryWin,{'afterQuery':function(responseText){//after queried,renew the page toolbar
			var after_query_span=Ext.get('g_pageSpan_info_'+ajaxTable).dom;
			var after_query_param=Ext.decode(after_query_span.innerText)
			after_query_param=Ext.apply({spanName:'g_pageSpan_'+ajaxTable},after_query_param);
		    globalScope._pageSpan(after_query_param);   		            
	}});	
	globalScope.queryWin.add(ajaxTable,queryWin);
	
	var editForm = new Ext.quick.QuickForm({
		id:ajaxTable+'_form',
		labelAlign: 'right',
		title: "<c:out value='${model.title}'/>",
		frame:true,
	url:'form.jsp'
	},formColumns,2);
	globalScope.form.add(ajaxTable,editForm);	    
	
	Ext.get('ajax_edit_win').dom.innerHTML="";
	var editWin = new Ext.Window({
		layout:'fit',
		el:'ajax_edit_win',
		width:800,
		height:600,
		closeAction:'hide',
		buttonAlign:'center',
		items: [editForm],
		buttons: [{
		text:'<fmt:message key="button.conf"/>',
		tabIndex:editForm.lastTabIndex,
		handler:function(){
				var r=null;
				if(globalScope.opt.get(ajaxTable)=='updt'){
	                if(Ext.isEmpty(editWin.updtHandle)==false){
	                  editWin.updtHandle(editForm);
	                  editWin.hide();
	                  return;
	                }
					r=richGrid.getSelectionModel().getSelected();
					editForm.getForm().updateRecord(r);
				}else if(globalScope.opt.get(ajaxTable)=='add'){
	                if(Ext.isEmpty(editWin.addHandle)==false){
	                  editWin.addHandle(editForm);
	                  editWin.hide();
	                  return;
	                }
					var r=editForm.newRecord(Ext.quick.globalScope.preid);
					var store=richGrid.getStore();
					richGrid.stopEditing();
					store.insert(0,r); 
					richGrid.startEditing(0,0);
					r.dirty=true;
					r.modified=r.data;
					if(store.modified.indexOf(r)==-1){
						store.modified.push(r);	
					}
				}
				editWin.hide();
		}
		},{
			text:'<fmt:message key="button.close"/>',
			tabIndex:editForm.lastTabIndex+1,
			handler:function(){editWin.hide()}
		}]
    });
	editWin.render(document.body);
	globalScope.win.add(ajaxTable,editWin);
	queryWin.render(document.body);
}
</script>